<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Delete File
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 06.10.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxDeleteDatablock
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 06.10.2005
    * @copyright essentialmind.com 2005
    ***/
    class ajaxDeleteDatablock extends ajaxAction{

        public function process(){
            
            $nodeid = (int)$_REQUEST['node'];
            $pageid = (int)$_REQUEST['page'];
            $block  = (int)$_REQUEST['id'];
            
            //now check if there is already a property with that name at that level
            if($pageid > 0){
                $this->db->Execute("DELETE FROM datablock_page_mapping WHERE pageid = $pageid AND blockid = $block");
                
            }else{
                $this->db->Execute("DELETE FROM datablock_node_mapping WHERE nodeid = $nodeid AND blockid = $block");
                
            }
            
            //get some info about the deleted datablock
            $data = $this->db->getRow("SELECT title, id, global, tag FROM datablocks WHERE id = $block");

            $this->r->id     = $data['id'];
            $this->r->title  = $data['title'];
            $this->r->tag    = $data['tag'];
            
            //if($data["global"] == 't'){
                $this->r->global = true; //keep this for a while
            //}
            
            //an interesting idea, now I eant to find out if there
            //are any inheretances that match the deleted block
            $myTree = new tree($this->db,'structure LEFT JOIN objectmap ON (structure.id = objectmap.nodeid)',null,'id, url, level, title');
            $path   = $myTree->getPathStack($nodeid,true);
            
            $blocks   = array();
            $myBlocks = array();
            foreach((array)$path as $value){
                $blocks = $this->db->getAssoc("SELECT 
                                                    datablocks.tag AS key, 
                                                    datablocks.id AS id, 
                                                    datablocks.title AS title,
                                                    datablock_node_mapping.nodeid AS node,
                                                    datablocks.tag AS tag
                                                FROM datablock_node_mapping AS dnm, datablocks
                                                WHERE dnm.inherit = TRUE
                                                AND dnm.blockid = datablocks.id
                                                AND datablocks.tag = '".$data['tag']."'
                                                AND dnm.nodeid = ".$value['id']);
                
                $myBlocks = array_merge($myBlocks,$blocks);
            }
            
            if(!empty($myBlocks)){
                $this->r->inherit = array_pop($myBlocks);
            }
        }
    }
?>